package org.jianghu.activity.user.regist.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import org.jianghu.R;
import org.jianghu.activity.user.OtherLoginView;
import org.jianghu.activity.user.OtherPresenterCompl;
import org.jianghu.activity.user.regist.presenter.RegistPresenterCompl;
import org.jianghu.base.BaseActivity;
import org.jianghu.utils.ToastUtil;
import org.jianghu.view.CircleImageView;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by Administrator on 2016/12/21.
 * 注册
 */
@ContentView(R.layout.activity_regist)
public class RegistActivity extends BaseActivity implements OtherLoginView, RegistView {

    @ViewInject(R.id.img_qq)
    private CircleImageView imgQq;
    private OtherPresenterCompl otherPresenterCompl;
    private RegistPresenterCompl registPresenterCompl;
    @ViewInject(R.id.btn_regist)
    private Button btn_regist;

    @ViewInject(R.id.btn_get_yzm)
    private Button btn_get_yzm;

    @ViewInject(R.id.edt_phone)
    private EditText edt_phone;

    @ViewInject(R.id.edt_pwd)
    private EditText edt_password;

    @ViewInject(R.id.btn_close)
    private ImageView img_close;

    public RegistActivity() {
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!TextUtils.isEmpty(edt_password.getText().toString())
                    && TextUtils.isEmpty(edt_phone.getText().toString())) {

                btn_regist.setEnabled(true);
                btn_regist.setBackgroundColor(getResources().getColor(R.color.color_2095f2));
            } else if (!TextUtils.isEmpty(edt_phone.getText().toString())) {
                btn_get_yzm.setEnabled(true);
                btn_get_yzm.setBackgroundColor(getResources().getColor(R.color.color_2095f2));
            } else {
                btn_get_yzm.setEnabled(false);
                btn_regist.setEnabled(false);
                btn_regist.setBackgroundColor(getResources().getColor(R.color.hui_68));
                btn_get_yzm.setBackgroundColor(getResources().getColor(R.color.hui_68));
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    @Override
    public void initViewsAndValues(Bundle savedInstanceState) {
        x.view().inject(this);

        editWatcher();
    }

    @Override
    public void initParmers() {
        otherPresenterCompl = new OtherPresenterCompl(this, this);
        registPresenterCompl = new RegistPresenterCompl(this);
    }

    @Override
    public void releaseOnDestory() {

    }

    @Event(value = {R.id.img_qq, R.id.btn_regist, R.id.btn_close})
    private void onclickListner(View view) {
        switch (view.getId()) {
            case R.id.img_qq:
                otherPresenterCompl.qqLogin();
                break;

            case R.id.btn_regist:
                registPresenterCompl.dnRegist(edt_phone.getText().toString(), edt_password.getText().toString());
                break;

            case R.id.btn_close:
                finish();
                break;
        }
    }

    @Override
    public void resultQqLogin(boolean isQqLogin) {
        if (isQqLogin) {
            ToastUtil.show(this, "QQ登陆成功", Toast.LENGTH_SHORT);
        } else {
            ToastUtil.show(this, "QQ登陆失败", Toast.LENGTH_SHORT);
        }
    }

    @Override
    public void resultSinaLogin(boolean isSinaLogin) {

    }

    @Override
    public void resltRegist(boolean isRegist) {
        if (isRegist) {
            ToastUtil.show(this, "注册成功", Toast.LENGTH_SHORT);
        } else {
            ToastUtil.show(this, "注册失败", Toast.LENGTH_SHORT);
        }
    }

    private void editWatcher() {
        btn_regist.setBackgroundColor(getResources().getColor(R.color.hui_68));
        btn_regist.setEnabled(false);
        edt_password.addTextChangedListener(textWatcher);
        edt_phone.addTextChangedListener(textWatcher);
    }
}
